package com.gm.commonlib.views.toast;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.gm.commonlib.R;
import com.gm.commonlib.application.MyApplicationHolder;

public class DefaultToast implements IToast{
    private DefaultToast(){}
    private static DefaultToast mDefaultToast=new DefaultToast();
    public static DefaultToast getDefaultToast(){
        return mDefaultToast;
    }
    private  Toast toast;
    private  Toast mCustomToast;
    /**
     * 显示一个文本并且设置时长
     *
     * @param msg
     */
    public  void showDefaultToast(final CharSequence msg,int time) {
//        if (TextUtils.isEmpty(msg)) {
//            return;
//        }
//        if (toast != null) {
//            toast.cancel();
//        }
//        Context context = MyApplicationHolder.getApplication().getContext();
//        if (context != null) {
//             toast = Toast.makeText(context, msg, time);
//            //todo 重新赋值，防止小米手机 带上应用名称的问题
//            toast.setText(msg);
//            toast.show();
//        }
        showCustomToast(msg.toString());

    }

    public void showCustomToast(String message){
        if (mCustomToast!=null){
            mCustomToast.cancel();
            mCustomToast=null;
        }
            Context context = MyApplicationHolder.getApplication().getContext();
            View view = LayoutInflater.from(context).inflate(R.layout.view_toast, null);
            TextView textView =  view.findViewById(R.id.toast_text);
            textView.setText(message);
            mCustomToast = new Toast(context);
            mCustomToast.setDuration(Toast.LENGTH_SHORT);
            mCustomToast.setGravity(Gravity.CENTER, 0, 0);
            mCustomToast.setView(view);
           mCustomToast.show();
    }


    @Override
    public void showToast(String text) {
        showDefaultToast(text,Toast.LENGTH_LONG);
    }

    @Override
    public void showToast(String text, int time) {
        showDefaultToast(text,time);
    }

    @Override
    public void showToast(int id) {

    }

}
